/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mfd2.frames;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import fr.ove.openmath.mfd2.*;
import fr.ove.openmath.mfd2.events.*;

/**
* A frame to display the OpenMath object exchanged by the application.
*
* @author © 1999 DIRAT Laurent
* @version 1.0  27/10/1999
*/
public class ShowOM extends Frame implements PanelJomesListener, RequestContainingListener {
	TextArea openMathArea;
	Button sendRequest;
	PanelJomes panelJomes;
	String type;
	
	public ShowOM(String type) {
		super("The OpenMath Syntax");
		setLayout(new BorderLayout(0, 0));
		setSize(310,360);
		
		this.type = type;
		
		// Le listener pour pouvoir fermer la fenêtre
		addWindowListener(
		    new WindowAdapter() {
        		public void windowClosing(WindowEvent event) {
                    dispose();
        		}
        	}
        );
		
		
		Font font = new Font("Dialog", Font.PLAIN, 14);
		
		// La zone OpenMath
		openMathArea = new TextArea();
		openMathArea.setFont(font);
		add("Center", openMathArea);
		
    	Label title;
		// Le titre dans la frame
		if (type.equals("Request")) {
    		title = new Label("The OpenMath Object of the Request", Label.CENTER);
    		
    		// Le bouton d'envoi de la requête
    		sendRequest = new Button();
    		sendRequest.setLabel("Set New Request");
    		sendRequest.setFont(font);
    		sendRequest.setBackground(new Color(12632256));
    		add("South", sendRequest);
    		
    		// Le traitement des actions sur le bouton
    		sendRequest.addActionListener(
    		    new ActionListener() {
    		        public void actionPerformed(ActionEvent event) {
    		            // A voir, éventuellement ce qu'il faut pour envoyer la requête
    		            // à la BD.
                        try {
                            panelJomes.setOpenMathRequest(openMathArea.getText());
                        }
                    	catch(Exception e) {
                    	    e.printStackTrace();
                        }
                    }    		
                }
            );
    	}
        else {
    		title = new Label("The OpenMath Object of the Result", Label.CENTER);
    		openMathArea.setEditable(false);
    	}
    		
		title.setFont(font);
		title.setBackground(new Color(12632256));
		add("North", title);
		
	}	

    /**
    */
    public void consumePanelJomesEvent(PanelJomesEvent panelJomesEvent) {
        panelJomes = (PanelJomes) panelJomesEvent.getSource();
        
        int action = panelJomesEvent.getAction();
        switch (action) {
            case PanelJomesEvent.UPDATE_REQUEST :
                if (type.equals("Request"))
                    openMathArea.setText(panelJomes.getOpenMathRequest());
                    
                break;
            case PanelJomesEvent.UPDATE_RESULT :
                if (type.equals("Result"))
                    openMathArea.setText(panelJomes.getOpenMathResult());
                    
                break;
            case PanelJomesEvent.UPDATE :
                if (type.equals("Request"))
                    openMathArea.setText(panelJomes.getOpenMathRequest());
                else
                    openMathArea.setText(panelJomes.getOpenMathResult());
        }
    }
    
    /**
    * Consumes (i.e. treats) the event received.
    * @param requestContainingEvent the event to consume.
    */
    public void consumeRequestContainingEvent(RequestContainingEvent requestContainingEvent) {
        RequestContaining source = (RequestContaining) requestContainingEvent.getSource();
        
        int action = requestContainingEvent.getAction();
        switch (action) {
            case RequestContainingEvent.UPDATE_REQUEST :
                if (type.equals("Request"))
                    openMathArea.setText(source.getOpenMathRequest());
                    
                break;
            case RequestContainingEvent.UPDATE_RESULT :
                if (type.equals("Result"))
                    openMathArea.setText(source.getOpenMathResult());
                    
                break;
        }
    }        
}


